import relayNode from "../relay";
import {GraphQLObjectType, GraphQLID} from "graphql";
import { attributeFields, resolver } from 'graphql-sequelize';
import { Profile, Ssid, Vlan } from "../models";
import SsidType from "./SsidType";
import {connectionFromModel, connectionFromType} from "../connection";
import {connectionArgs} from "graphql-relay";
import VlanType from "./VlanType";

const ProfileType = new GraphQLObjectType({
  name: "profile",
  fields: () => Object.assign(
    {},
    attributeFields(Profile, {
      exclude: [],
      globalId: true,
      allowNull: true,
    }),
    {
      ssids: {
        type: connectionFromType('ssids', SsidType).connection,
        args: {
          ...connectionArgs,
        },
        resolve: async (profile, { ...args }) => {
          return connectionFromModel(Ssid, {
            where: {
              profile_id: profile.id,
            },
          }, args);
        }
      },
      vlans: {
        type: connectionFromType('vlans', VlanType).connection,
        args: {
          ...connectionArgs,
        },
        resolve: async (profile, { ...args }) => {
          return connectionFromModel(Vlan, {
            where: {
              profile_id: profile.id,
            },
          }, args);
        }
      },
    },
  ),
  interfaces: [relayNode.nodeInterface],
});

export default ProfileType;
